package com.examples.database;

import java.util.ArrayList;

import com.examples.Model.AppoinModel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class AppoinAdapter {

	 public static final String KEY_ROWID = "_id";
	 public static final String KEY_AGENDA = "agenda";
	 public static final String KEY_PERSON = "person";
	 public static final String KEY_DESCRIPTION = "description";
	 public static final String KEY_DATE = "date";
	 public static final String KEY_TIME = "time";
	 public static final String KEY_REMINDER_DATE = "reminderdate";
	 public static final String KEY_REMINDER_TIME = "remindertime";
	 //public static final String KEY_LOCATION = "location";
	 private static final String DATABASE_TABLE = "details";
	 
	 
	 private Context context;
	 private SQLiteDatabase database;
	 private Appointment dbHelper;

		    public AppoinAdapter(Context context) {
	
		        this.context = context;
		    }
	
		    public AppoinAdapter open() throws SQLException {
	
		        dbHelper = new Appointment(context);
		        database = dbHelper.getWritableDatabase();
		        return this;
		    }
	
		    public void close() {

		        dbHelper.close();
		    }
		    /**
	         * Create a new Appointment If the Appointment is successfully created return the new
	         * rowId for that note, otherwise return a -1 to indicate failure.
	         */
		    public long createAppointment(AppoinModel item) {

		        ContentValues initialValues = createContentValues(item);

		        return database.insert(DATABASE_TABLE, null, initialValues);
		    }

		    public ContentValues createContentValues(AppoinModel items) {

		        ContentValues values = new ContentValues();
		        values.put(KEY_AGENDA, items.agenda);
		        values.put(KEY_PERSON, items.person);
		        values.put(KEY_DESCRIPTION, items.description);
		        values.put(KEY_DATE, items.date);
		        values.put(KEY_TIME, items.time);
		        values.put(KEY_REMINDER_DATE, items.reminderdate);
		        values.put(KEY_REMINDER_TIME, items.remindertime);
		        //values.put(KEY_LOCATION, items.location);
		        
		        return values;
		    }

			public Cursor fetchAllAppoin() {
				return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
						 KEY_AGENDA, KEY_PERSON,KEY_DESCRIPTION, KEY_DATE, KEY_TIME,KEY_REMINDER_DATE, KEY_REMINDER_TIME}, null, null, null,
							null, null);
				
									
			}
			
			public Cursor fetchAllGroups(){
				String colNames[]={KEY_AGENDA};
				dbHelper.onCreate(database);
				return database.query(DATABASE_TABLE , colNames, null, null, null, null, null);
			}
			public ArrayList<AppoinModel> fetchGroups(){
				ArrayList<AppoinModel> list=new ArrayList<AppoinModel>();
				Cursor c=fetchAllGroups();
				if(c!=null){
					while(c.moveToNext()){
						
						String name=c.getString(c.getColumnIndexOrThrow(KEY_AGENDA));
						
						Log.d("Note id",name);
						
						AppoinModel g=new AppoinModel();
						g.setAgenda(name);
						//Note g=new Note(name,id);
						list.add(g);
					}
				}
				c.close();
				return list;
			}

			public void deleteGroup(Integer groupId) {
				database.delete(DATABASE_TABLE,  KEY_ROWID + "=" + groupId , null);
				
			}
			

}
